<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>httpPost</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        :root {
            --borderColor--: #dededf;
            --leftColor--: rgba(0, 0, 0, .85);
            --rightColor--: #8593a1;
        }

        .content {
            padding: 15px;
        }

        .row {
            display: flex;
            align-items: center;
            margin-bottom: 8px;
            justify-content: flex-start;
            border: 1px solid var(--borderColor--);
            padding: 8px 12px;
            height: 64px;
            border-radius: 6px;
            font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
        }

        .row-left {
            margin-right: 8px;
            text-align: right;
            flex: 0 1 80px;
            color: var(--leftColor--);
            font-size: 18px;
        }

        .row-right {
            flex: 1;
            color: var(--rightColor--);
            font-size: 14px;
        }

        .title {
            color: var(--leftColor--);
            font-size: large;
            font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
            text-align: center;
            margin: 15px 0;
        }
    </style>
</head>

<body>
    <h1 class="title">The request data</h1>
    <div id="content" class="content"></div>
    <script>
        const content = document.getElementById('content');
        const httpPost = (url, data, callback, error = console.error) => {
            const request = new XMLHttpRequest();
            request.open('POST', url, true);
            request.setRequestHeader('Content-type', 'application/json; charset=utf-8');
            request.onload = () => callback(request.responseText);
            request.onerror = () => error(request);
            request.send(data);
        }
        const newPost = {
            userId: 1,
            id: 1337,
            title: 'sdgdsgsdgsdgsdgdsgdsgsdg',
            body: 'sdgdsgsdgsdgsdgdsgdsgsdgsdgdsgsdgsdgsdgdsgdsgsdgsdgdsgsdgsdgsdgdsgdsgsdgsdgdsgsdgsdgsdgdsgdsgsdgsdgdsgsdgsdgsdgdsgdsgsdg'
        };
        const data = JSON.stringify(newPost);
        httpPost(
            'https://jsonplaceholder.typicode.com/posts/',
            data,
            (text) => {
                console.log(text);
                try {
                    const data = JSON.parse(text);
                    console.log(data);
                    let template = "";
                    for (const key in data) {
                        template += `
                            <div class="row">
                                <label class="row-left">${ key }:</label>
                                <p class="row-right">${ data[key] }</p>    
                            </div>
                        `
                    }
                    content.innerHTML = template;
                } catch (error) {
                    console.error(error);
                }
            }
        );
    </script>
</body>

</html>